using System.Linq;
using MvcDemo.Models;
using MvcDemo.Db;
using Microsoft.EntityFrameworkCore;
using System;
namespace MvcDemo.Repost
{
    public class EfRepost<T> : IRepost<T> where T : BaseEntity
    {
        private readonly Demo _db;
        private readonly DbSet<T> _table;

        public IQueryable<T> Table
        {
            get{
                return _table.AsQueryable<T>();
            }
        }

        public EfRepost ()
        {
            _db=new Demo();
            _table=_db.Set<T>();
        }
        public void Delete(int id)
        {
            var entity=_table.Where(x=>x.Id==id).FirstOrDefault();
            entity.IsDeleted=true;
            entity.UpdatedTime=DateTime.Now;
            _table.Update(entity);
            _db.SaveChanges();
        }

        public void Insert(T entity)
        {
            entity.IsActived=true;
            entity.IsDeleted=false;
            entity.CreatedTime=DateTime.Now;
            _table.Add(entity);
            _db.SaveChanges();
        }

        public void Update(T entity)
        {
            entity.UpdatedTime=DateTime.Now;
            _table.Update(entity);
            _db.SaveChanges();
        }
    }
}